// com.jinke.game.Explosion.java
package com.jinke.game;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Explosion {
    // 动画帧数
    public static final int ANIMATION_STEPS = 16;

    private int x, y;
    private int currentStep; // 当前动画步骤
    private boolean active; // 是否激活状态
    private static Image[] img;

    // 静态初始化，加载爆炸图片
    static {
        try {
            img = new Image[ANIMATION_STEPS/4];
            for (int i = 0; i < img.length; i++) {
                img [i] = ImageIO.read(Objects.requireNonNull(Explosion.class.getClassLoader().getResourceAsStream("img/boom_" + i + ".png")));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Explosion() {
        this.active = false;
    }

    public void init(int x, int y) {
        this.x = x-img[0].getWidth(null)/2;
        this.y = y-img[0].getHeight(null)/2;
        this.currentStep = 0;
        this.active = true;
    }


    public void draw(Graphics g) {
        if (!active) return;

        int index = currentStep / 4;
        if (index >= img.length){
            index = img.length - 1;
        }
        if (index>=0&&img[index]!=null){
            g.drawImage(img[index], x, y, null);
        }
        currentStep++;
        if (currentStep >= ANIMATION_STEPS) {
            active = false;
        }
    }

    public boolean isActive() {
        return active;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}
